package base;


import aspect.DeleteOperationLog;
import aspect.InsertOperationLog;
import aspect.UpdateOperationLog;
import base.response.OffsetLimitPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.AopContext;
import org.springframework.transaction.annotation.Transactional;
import util.key.IDFactory;

import java.util.List;

/**
 * 实现BaseService抽象类
 * Created by admin on 2017/01/07.
 */
public abstract class IBaseServicesImpl<Record extends IBaseEntity, RecordWithBLOBs extends Record, Example> implements IBaseServices<Record, RecordWithBLOBs, Example> {
    private static Logger log = LoggerFactory.getLogger(IBaseServicesImpl.class);

    public abstract   IBaseMapper<Record, RecordWithBLOBs, Example> getMapper();

    public  abstract  String  getTableName();

    public IBaseServices getCurrentProxy() {
        return ((IBaseServices) AopContext.currentProxy());
    }


    public int countByExample(Example example) throws Exception {
        return getMapper().countByExample(example);
    }


    public List<Record> selectByExample(Example example) throws Exception {
        return getMapper().selectByExample(example);
    }

    @Transactional
    public RecordWithBLOBs selectByPrimaryKey(String id) throws Exception {
        return getMapper().selectByPrimaryKey(id);
    }

    @DeleteOperationLog
    public int deleteByExample(Example example) throws Exception {
        return getMapper().deleteByExample(example);
    }

    @DeleteOperationLog
    public int deleteByPrimaryKey(String id) throws Exception {
        return getMapper().deleteByPrimaryKey(id);
    }

    @InsertOperationLog
    public int insert(RecordWithBLOBs record) throws Exception {
        if (record.getId() == null || "".equals(record.getId())) {
            record.setId(IDFactory.getSnowflakeId());
        }
        return getMapper().insert(record);
    }

    @InsertOperationLog
    public int insertSelective(RecordWithBLOBs record) throws Exception {
        if (record.getId() == null || "".equals(record.getId())) {
             record.setId(IDFactory.getSnowflakeId());
        }
        return getMapper().insertSelective(record);
    }

    @UpdateOperationLog
    public int updateByExampleSelective(@Param("record") RecordWithBLOBs record, @Param("example") Example example) throws Exception {
        return getMapper().updateByExampleSelective(record, example);
    }

    @UpdateOperationLog
    public int updateByExample(@Param("record") Record record, @Param("example") Example example) throws Exception {
        return getMapper().updateByExample(record, example);
    }

    @UpdateOperationLog
    public int updateByPrimaryKeySelective(RecordWithBLOBs record) throws Exception {
        return getMapper().updateByPrimaryKeySelective(record);
    }


    @UpdateOperationLog
    public int updateByPrimaryKey(Record record) throws Exception {
        return getMapper().updateByPrimaryKey(record);
    }


    public OffsetLimitPage selectByExample(Example example, Integer offset, Integer limit) throws Exception {
        PageHelper.offsetPage(offset, limit);
        List<Record> recordList = getMapper().selectByExample(example);
        return new OffsetLimitPage((Page<Record>) recordList);
    }




}